package com.pdg.mcplugin.varnish;

import org.bukkit.Material;
import org.bukkit.TreeSpecies;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.material.MaterialData;

import com.pdg.mcplugin.common.baseclasses.PluginClientBase;

public class RecipeMaker extends PluginClientBase<Varnish>{

	private static final int MINIMUM_COUNT = 1;
	public static final int MAXIMUM_COUNT = 8;
	public static final MaterialData WHITE_DYE = new MaterialData(Material.INK_SACK,(byte) 15);
	public static final MaterialData BROWN_DYE = new MaterialData(Material.INK_SACK,(byte) 3);

	public RecipeMaker(Varnish thePlugin) {
		super(thePlugin);
	}

	public void makeRecipes() {
		makePlankRecipes();
		makeSlabRecipes();
		makeStairRecipes();
	}

	private void makeStairRecipes() {
		makeBleachedStairRecipes();
		makeStainedStairRecipes();
	}

	private void makeStainedStairRecipes() {
		if(!getPlugin().getFeatureManager().getFeature(VarnishFeatures.RECIPE_STAIN_STAIRS)) return;
		makeStainedRecipe(Material.BIRCH_WOOD_STAIRS,Material.WOOD_STAIRS);
		makeStainedRecipe(Material.WOOD_STAIRS,Material.JUNGLE_WOOD_STAIRS);
		makeStainedRecipe(Material.JUNGLE_WOOD_STAIRS,Material.SPRUCE_WOOD_STAIRS);
	}

	private void makeStainedRecipe(Material theInputMaterial, Material theOutputMaterial) {
		makeStainedRecipe(new MaterialData(theInputMaterial),new MaterialData(theOutputMaterial));
	}

	private void makeStainedRecipe(MaterialData theInputMaterial, MaterialData theOutputMaterial) {
		makeRecipe(theInputMaterial,theOutputMaterial,BROWN_DYE);
	}
	
	private void makeRecipe(MaterialData theInputMaterialData,
			MaterialData theOutputMaterialData, MaterialData theCatalystMaterialData) {
		for(int theCount=MINIMUM_COUNT;theCount<=MAXIMUM_COUNT;++theCount){
			ItemStack theResult = theOutputMaterialData.toItemStack(theCount);
			ShapelessRecipe theRecipe = new ShapelessRecipe(theResult);
			theRecipe.addIngredient(1,theCatalystMaterialData);
			theRecipe.addIngredient(theCount,theInputMaterialData);
			getPlugin().getServer().addRecipe(theRecipe);
		}		
	}

	private void makeBleachedRecipe(MaterialData theInputMaterial, MaterialData theOutputMaterial){
		makeRecipe(theInputMaterial,theOutputMaterial,WHITE_DYE);
	}

	private void makeBleachedStairRecipes() {
		if(!getPlugin().getFeatureManager().getFeature(VarnishFeatures.RECIPE_BLEACH_STAIRS)) return;
		makeBleachedRecipe(Material.WOOD_STAIRS,Material.BIRCH_WOOD_STAIRS);
		makeBleachedRecipe(Material.JUNGLE_WOOD_STAIRS,Material.WOOD_STAIRS);
		makeBleachedRecipe(Material.SPRUCE_WOOD_STAIRS,Material.JUNGLE_WOOD_STAIRS);
	}

	private void makeBleachedRecipe(Material theInputMaterial,
			Material theOutputMaterial) {
		makeBleachedRecipe(new MaterialData(theInputMaterial),new MaterialData(theOutputMaterial));
	}

	private void makeSlabRecipes() {
		makeBleachedSlabRecipes();
		makeStainedSlabRecipes();
	}

	private void makeStainedSlabRecipes() {
		if(!getPlugin().getFeatureManager().getFeature(VarnishFeatures.RECIPE_STAIN_STEPS)) return;
		makeStainedRecipe(Material.WOOD_STEP,TreeSpecies.BIRCH,TreeSpecies.GENERIC);
		makeStainedRecipe(Material.WOOD_STEP,TreeSpecies.GENERIC,TreeSpecies.JUNGLE);
		makeStainedRecipe(Material.WOOD_STEP,TreeSpecies.JUNGLE,TreeSpecies.REDWOOD);
	}

	private void makeStainedRecipe(Material theMaterial, TreeSpecies theInputTreeSpecies, 
			TreeSpecies theOutputTreeSpecies) {
		makeStainedRecipe(new MaterialData(theMaterial, theInputTreeSpecies.getData()),new MaterialData(theMaterial,theOutputTreeSpecies.getData()));
	}

	private void makeBleachedSlabRecipes() {
		if(!getPlugin().getFeatureManager().getFeature(VarnishFeatures.RECIPE_BLEACH_STEPS)) return;
		makeBleachedRecipe(Material.WOOD_STEP,TreeSpecies.GENERIC,TreeSpecies.BIRCH);
		makeBleachedRecipe(Material.WOOD_STEP,TreeSpecies.JUNGLE,TreeSpecies.GENERIC);
		makeBleachedRecipe(Material.WOOD_STEP,TreeSpecies.REDWOOD,TreeSpecies.JUNGLE);
	}

	private void makeBleachedRecipe(Material theMaterial, TreeSpecies theInputTreeSpecies, 
			TreeSpecies theOutputTreeSpecies) {
		makeBleachedRecipe(new MaterialData(theMaterial, theInputTreeSpecies.getData()),new MaterialData(theMaterial,theOutputTreeSpecies.getData()));
	}

	private void makePlankRecipes() {
		makeBleachedPlankRecipes();
		makeStainedPlankRecipes();
	}

	private void makeStainedPlankRecipes() {
		if(!getPlugin().getFeatureManager().getFeature(VarnishFeatures.RECIPE_STAIN_PLANKS)) return;
		makeStainedRecipe(Material.WOOD,TreeSpecies.BIRCH,TreeSpecies.GENERIC);
		makeStainedRecipe(Material.WOOD,TreeSpecies.GENERIC,TreeSpecies.JUNGLE);
		makeStainedRecipe(Material.WOOD,TreeSpecies.JUNGLE,TreeSpecies.REDWOOD);
	}

	private void makeBleachedPlankRecipes() {
		if(!getPlugin().getFeatureManager().getFeature(VarnishFeatures.RECIPE_BLEACH_PLANKS)) return;
		makeBleachedRecipe(Material.WOOD,TreeSpecies.GENERIC,TreeSpecies.BIRCH);
		makeBleachedRecipe(Material.WOOD,TreeSpecies.JUNGLE,TreeSpecies.GENERIC);
		makeBleachedRecipe(Material.WOOD,TreeSpecies.REDWOOD,TreeSpecies.JUNGLE);
	}

}
